<?php

namespace App\Http\Middleware;
use Closure;
class AdminAuthenticate
{


    /**
     * 功能：验证登录权限
     *
     * @param  string token header请求头传输
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->header('token')) {
            $request['token'] = $request->header('token');
        }
        $admin = auth()->guard('admin')->user();
        if (!$admin) {
            return response()->json(['code' => 4001, 'msg' => '未登录授权！', 'data' => []]);
        }
        if ($admin->status == 2){ // 冻结
            return response()->json(['code' => 4001, 'msg' => '账号被冻结请联系超管！', 'data' => []]);
        }
        $this->addLog($admin,$request);
        return $next($request);
    }

    public function addLog($admin,$request){
        $log = [
            'type'    => 1,
            'user_id' => $admin->id,
            'path'    => $request->path(),
            'method'  => $request->method(),
            'ip'      => $request->getClientIp(),
            'input'   => json_encode($request->input()),
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s')
        ];
        \DB::table('operate_log')->insert($log);
    }
}
